package neuron;

import java.util.ArrayList;
import java.util.List;

public class Soma {
	
	/**
	 * Path bounding the area of soma
	 */
	private List<Coord> points;

	
	public Soma() {
	    points = new ArrayList<Coord>();
	}
	
	public Soma(List<Coord> points) {
	    this.points = points;
	}
	
	public Soma(Coord center) {
		this.points = new ArrayList<Coord>();
		points.add(center);
	}

	/**
	 * Return center point of soma.
	 * 
	 * @return
	 */
	public Coord center()
	{
		if (points == null || points.size() == 0) return null;
		if (points.size() == 1) return points.get(0);
		
		double x=0,y=0,z=0;
		for (Coord c : points) {
			x += c.x; y += c.y; z += c.z;
		}
		int n = points.size();
		return new Coord(x/n, y/n, z/n, 0);
	}
	
	/**
	 * Compute 2D (i.e. planar) area of soma.
	 * 
	 * Note: if soma is not defined as a path, return value will be 0.
	 * @return
	 */
	public double planarArea()
    {
		if (points == null || points.size() < 3) return 0;
		
        double planeZ = points.get(0).z;
        for (Coord c : points) if (c.z != planeZ) return -1; // ERROR not planar

        double sum = 0;
        for (int i = 0; i < points.size(); i++) {
            Coord a = points.get(i);
            Coord b = points.get((i+1)%points.size());
            sum += a.x * b.y - b.x * a.y;
        }
        return 0.5 * sum;
    }

	public List<Coord> getPoints() {
		return points;
	}
}
